/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package who.tmap.ui.util;

import java.awt.Color;
import java.awt.Component;
import java.io.File;
import java.net.JarURLConnection;
import java.net.URL;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.Manifest;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;

import org.apache.log4j.Logger;

import who.tmap.ui.JFrameHelp;
import who.tmap.util.MiscHelper;

public class MiscUIHelper {

    private static Logger logger = Logger.getLogger(MiscUIHelper.class);	

    private static JFrameHelp jFrameHelp =null;
    
	public static Color TABLE_CELL_BG_COLOR =new Color( 194, 213, 232 );     
	
    public void displayErrorMessage( Component parent, Exception e ) {
        logger.error( e );
     
        displayErrorMessage( parent, e.getMessage() );
    }
    
    public void displayErrorMessage( Component parent, String msg ) {	
        JOptionPane.showMessageDialog( parent, msg, "TMap error", JOptionPane.ERROR_MESSAGE );   
    } 
    
    public void displayWarningMessage( Component parent, String msg ) {
        JOptionPane.showMessageDialog( parent, msg, "TMap warning", JOptionPane.WARNING_MESSAGE ); 
    }
    
    public void showHelp() {
		if ( jFrameHelp == null ) {
			jFrameHelp =new JFrameHelp();
		}
		
		jFrameHelp.setVisible( true );
    }
    
    public void closeHelp() {
		if ( jFrameHelp != null ) {
			jFrameHelp.dispose();
		}
		
		jFrameHelp =null;
    }
    
    public File openBitmapFile() {
    	File file =null;
    	
		JFileChooser fileChooser = new JFileChooser();
		FileFilter defaultFileFilter =new FileChooserFilter( FileChooserFilter.GIF_EXT );
		fileChooser.addChoosableFileFilter( defaultFileFilter );
		fileChooser.addChoosableFileFilter( new FileChooserFilter( FileChooserFilter.JPG_EXT ) );
		fileChooser.addChoosableFileFilter( new FileChooserFilter( FileChooserFilter.PNG_EXT ) );	
		
		fileChooser.setFileFilter( defaultFileFilter );

		// Open file dialog.
		int retVal =fileChooser.showOpenDialog(null);
		
		if ( retVal == JFileChooser.APPROVE_OPTION ) {		
			
		    file =fileChooser.getSelectedFile();
		    
		    if ( !file.exists() || file.isDirectory() ) {
		    	file =null;
		    }
		} 

		return file;
    }
    
    @SuppressWarnings("static-access")
	public String getBuildDate() {
    	String buildDate ="";
    	
    	try	{
	    	Manifest mf =null;

		    if ( new MiscHelper().isWebStart() ) {
			    String localFile = getClass().getProtectionDomain().getCodeSource().getLocation().toString().concat("!/");
		    	String tmpString = "jar:";
		    	String localJarFileString = tmpString.concat(localFile);
		    	URL localJarFileURL = new URL(localJarFileString);
		    	JarURLConnection localJarFile = (JarURLConnection)localJarFileURL.openConnection();
		    	
		    	mf = localJarFile.getManifest();
	    	} else {
	    		String jarFile =getClass().getProtectionDomain().getCodeSource().getLocation().getFile();
	    		mf =new JarFile( jarFile ).getManifest(); 
	    	}
	    	Attributes attr = mf.getMainAttributes();

	    	buildDate = attr.getValue("Build-Date"); 	    	
    	} catch (Exception e) {
    		logger.error(e);
    	}
    	
    	return buildDate;
    }   

	
}
